using System;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using Ventigrate.Shared.ExternalMembership.Internal;
using SWS = System.Web.Security;

namespace Ventigrate.Shared.ExternalMembership.WebControls
{
    /// <summary>
    /// Code behind for UserUnlock.aspx
    /// </summary>
    public class UserUnlock : LayoutsPageBase
    {
        protected Label unlockMsg;

        protected override bool RequireSiteAdministrator
        {
            get { return true; }
        }

        protected override void OnLoad(EventArgs e)
        {
            // init
            this.CheckRights();

            // display error confirmation message
            string userName = Request.QueryString["USERNAME"];
            if (!string.IsNullOrEmpty(userName))
            {
                SWS.MembershipUser user = Membership.GetUser(userName);
                if (user.IsLockedOut)
                {
                    unlockMsg.Text = "Confirm that you want to unlock the user '" + Request.QueryString["USERNAME"] + "'";
                }
                else
                {
                    SPUtility.TransferToErrorPage("User Not Locked Out");

                }
            }
            else
            {
                SPUtility.TransferToErrorPage("User Not Found");
            }
        }

        protected void OnUnlock(object sender, EventArgs e)
        {
            string userName = Request.QueryString["USERNAME"];

            try
            {
                // unlock user
                SWS.MembershipUser user = Membership.GetUser(userName);
                user.UnlockUser();
            }
            catch (Exception ex)
            {
                ULS.Log.Error(ex);
                SPUtility.TransferToErrorPage(ex.Message);                 
            }
                        
            Response.Redirect("UsersDisp.aspx", false);
        }

    }
}
